package com.xb.Demo01;


import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 只练习第五种方式来连接数据
 */
public class Demo02TestConnection {

    //方式五 读配置文件：
    @Test
    public void testConnection05() throws SQLException, ClassNotFoundException, IOException {
        //通过反射来加载读取文件
        InputStream resourceAsStream = Demo02TestConnection.class.getClassLoader().getResourceAsStream("mysqlConnectInfo.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String driver = properties.getProperty("driver");
        String user = properties.getProperty("user");
        String password =  properties.getProperty("password");
        String url =  properties.getProperty("url");
        //通过反射加载驱动
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();

    }
}
